package com.github.llyb120.workflow.definition;

import groovy.lang.Closure;
import java.util.*;
import static com.github.llyb120.workflow.definition.Helper.*;
//import static com.github.llyb120.workflow.definition.Helper.EventType.POST;
//import static com.github.llyb120.workflow.definition.Helper.EventType.PRE;


public class All {


    private final static ThreadLocal<LinkedList> local = new ThreadLocal<LinkedList>(){
        @Override
        protected LinkedList initialValue() {
            return new LinkedList();
        }
    };
    private final static ThreadLocal<LinkedList<String>> localPath = new ThreadLocal(){
        @Override
        protected LinkedList initialValue() {
            return new LinkedList();
        }
    };

    public static void workflow (Closure workflow){
        pushPathStack("workflow");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Workflow _workflow = new Workflow();
        Context context = new Context(localPath.get(), local.get());
        //@PRE(context)

        pushStack(_workflow);
        if(workflow != null){
            workflow .call();
        }
        //visit node
        //@POST(context)
        //for(Interceptor interceptor : Helper.interceptors){
        //    interceptor.visitWorkflow(new Context(localPath.get(), local.get()));
        //}
        localElement.get().add(_workflow);
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void node (Closure node){
        pushPathStack("node");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Node _node = new Node();
        Context context = new Context(localPath.get(), local.get());
        //@PRE(context)
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Workflow) {
            ((Workflow)item).getNode ().add( _node);
        }

        pushStack(_node);
        if(node != null){
            node .call();
        }
        //visit node
        //@POST(context)
        //for(Interceptor interceptor : Helper.interceptors){
        //    interceptor.visitNode(new Context(localPath.get(), local.get()));
        //}
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void id (String id){
        pushPathStack("id");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Workflow) {
            ((Workflow)item).setId (id);
        } else if( item instanceof Node) {
            ((Node)item).setId (id);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void node (String id){
        node (id,(Closure) null );
    }
    public static void node (String id,Closure node){
        pushPathStack("node");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Node _node = new Node();
        Context context = new Context(localPath.get(), local.get());
        //@PRE(context)
        _node .setId (id);
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Workflow) {
            ((Workflow)item).getNode ().add( _node);
        }

        pushStack(_node);
        if(node != null){
            node .call();
        }
        //visit node
        //@POST(context)
        //for(Interceptor interceptor : Helper.interceptors){
        //    interceptor.visitNode(new Context(localPath.get(), local.get()));
        //}
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void maximumHandlers (int maximumHandlers){
        pushPathStack("maximumHandlers");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Node) {
            ((Node)item).setMaximumHandlers (maximumHandlers);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void dealer (Closure dealer){
        pushPathStack("dealer");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Dealer _dealer = new Dealer();
        Context context = new Context(localPath.get(), local.get());
        //@PRE(context)
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Node) {
            ((Node)item).getDealer ().add( _dealer);
        }

        pushStack(_dealer);
        if(dealer != null){
            dealer .call();
        }
        //visit node
        //@POST(context)
        //for(Interceptor interceptor : Helper.interceptors){
        //    interceptor.visitDealer(new Context(localPath.get(), local.get()));
        //}
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void to (Object to){
        pushPathStack("to");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Node) {
            ((Node)item).getTo ().add(to);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void minimumHandlers (int minimumHandlers){
        pushPathStack("minimumHandlers");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Node) {
            ((Node)item).setMinimumHandlers (minimumHandlers);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void waitForMerge (boolean waitForMerge){
        pushPathStack("waitForMerge");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Node) {
            ((Node)item).setWaitForMerge (waitForMerge);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void maximumParallelNum (int maximumParallelNum){
        pushPathStack("maximumParallelNum");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Node) {
            ((Node)item).setMaximumParallelNum (maximumParallelNum);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void startnode (Closure startnode){
        pushPathStack("startnode");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Startnode _startnode = new Startnode();
        Context context = new Context(localPath.get(), local.get());
        //@PRE(context)
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Workflow) {
            ((Workflow)item).getNode ().add( _startnode);
        }

        pushStack(_startnode);
        if(startnode != null){
            startnode .call();
        }
        //visit node
        //@POST(context)
        //for(Interceptor interceptor : Helper.interceptors){
        //    interceptor.visitStartnode(new Context(localPath.get(), local.get()));
        //}
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void startnode (String id){
        startnode (id,(Closure) null );
    }
    public static void startnode (String id,Closure startnode){
        pushPathStack("startnode");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Startnode _startnode = new Startnode();
        Context context = new Context(localPath.get(), local.get());
        //@PRE(context)
        _startnode .setId (id);
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Workflow) {
            ((Workflow)item).getNode ().add( _startnode);
        }

        pushStack(_startnode);
        if(startnode != null){
            startnode .call();
        }
        //visit node
        //@POST(context)
        //for(Interceptor interceptor : Helper.interceptors){
        //    interceptor.visitStartnode(new Context(localPath.get(), local.get()));
        //}
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void test (String test){
        pushPathStack("test");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Startnode) {
            ((Startnode)item).setTest (test);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void endnode (Closure endnode){
        pushPathStack("endnode");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Endnode _endnode = new Endnode();
        Context context = new Context(localPath.get(), local.get());
        //@PRE(context)
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Workflow) {
            ((Workflow)item).getNode ().add( _endnode);
        }

        pushStack(_endnode);
        if(endnode != null){
            endnode .call();
        }
        //visit node
        //@POST(context)
        //for(Interceptor interceptor : Helper.interceptors){
        //    interceptor.visitEndnode(new Context(localPath.get(), local.get()));
        //}
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void endnode (String id){
        endnode (id,(Closure) null );
    }
    public static void endnode (String id,Closure endnode){
        pushPathStack("endnode");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Endnode _endnode = new Endnode();
        Context context = new Context(localPath.get(), local.get());
        //@PRE(context)
        _endnode .setId (id);
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Workflow) {
            ((Workflow)item).getNode ().add( _endnode);
        }

        pushStack(_endnode);
        if(endnode != null){
            endnode .call();
        }
        //visit node
        //@POST(context)
        //for(Interceptor interceptor : Helper.interceptors){
        //    interceptor.visitEndnode(new Context(localPath.get(), local.get()));
        //}
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void byRoleName (String[] byRoleName){
        pushPathStack("byRoleName");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Dealer) {
            ((Dealer)item).setByRoleName (byRoleName);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void to (Closure to){
        pushPathStack("to");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        To _to = new To();
        Context context = new Context(localPath.get(), local.get());
        //@PRE(context)
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Node) {
            ((Node)item).getTo ().add( _to);
        }

        pushStack(_to);
        if(to != null){
            to .call();
        }
        //visit node
        //@POST(context)
        //for(Interceptor interceptor : Helper.interceptors){
        //    interceptor.visitTo(new Context(localPath.get(), local.get()));
        //}
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void to (String name){
        to (name,(Closure) null );
    }
    public static void to (String name,Closure to){
        pushPathStack("to");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        To _to = new To();
        Context context = new Context(localPath.get(), local.get());
        //@PRE(context)
        _to .setName (name);
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Node) {
            ((Node)item).getTo ().add( _to);
        }

        pushStack(_to);
        if(to != null){
            to .call();
        }
        //visit node
        //@POST(context)
        //for(Interceptor interceptor : Helper.interceptors){
        //    interceptor.visitTo(new Context(localPath.get(), local.get()));
        //}
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void expression (Object expression){
        pushPathStack("expression");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof To) {
            ((To)item).setExpression (expression);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void name (String name){
        pushPathStack("name");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof To) {
            ((To)item).setName (name);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }



    private static Object pickStack(){
        return local.get().getLast();
    }
    private static void pushStack(Object obj){
        local.get().addLast(obj);
    }
    private static Object popStack(){
        return local.get().removeLast();
    }
    private static void pushPathStack(String name){
        localPath.get().addLast((name));
    }
    private static Object popPathStack(){
        return localPath.get().removeLast();
    }
    private static String getCurrentPath(){
        return String.join(".", localPath.get());
    }

}